package com.example.loginapp.dao;

import com.example.loginapp.model.User;
import java.util.Arrays;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestBody;

@Repository
public class UserDAO {
    static User[] USERS = new User[]{new User(1L, "lmj", "123",true), new User(2L, "lp", "234",true), new User(3L, "lb", "567",true)};

    public UserDAO() {
    }

    public boolean verify(@RequestBody String userName, String password) {
        User[] var3 = USERS;
        int var4 = var3.length;
        //System.out.println(2);
        for(int var5 = 0; var5 < var4; ++var5) {
            User user = var3[var5];
            if (user.getName().equals(userName) && user.getPassword().equals(password)) {
                return true;
            }
        }

        return false;
    }

    public boolean addverify(@RequestBody String userName, String password) {
        User[] newUSER = new User[USERS.length+1];
        for(int i=0;i<USERS.length+1;i++){
            if(i==USERS.length){
                newUSER[i] = new User(USERS.length+1,userName,password,true);
                break;
            }
            newUSER[i] = USERS[i];
        }
        USERS = newUSER;
        return true;
    }

    public boolean deleteverify(@RequestBody String userName, String password) {
        User[] newUSER = new User[USERS.length-1];
        User[] user = USERS;
        int ans = -1;
        int ans1 = 0;
        for(int i=0;i<USERS.length;i++){
            if((user[i].getName().equals(userName) || user[i].getPassword().equals(password))){
                ans = i;
            }
        }
        if(ans==-1){
            return false;
        }
        for(int i=0;i<USERS.length;i++){
            if(i==ans){
                continue;
            }
            newUSER[ans1++] = user[i];
        }
        USERS = newUSER;
        return true;
    }
    public boolean updataverify(@RequestBody String userName, String password) {
        User[] user = USERS;
        User[] newUSER = new User[user.length];
        int ans = -1;
        for(int i=0;i<user.length;i++){
            if(user[i].getName().equals(userName)){
                newUSER[i] = new User(user[i].getId(),userName,password,true);
                ans = 1;
                continue;
            }
            newUSER[i] = user[i];
        }
        USERS = newUSER;
        if(ans==1)return true;
        return false;
    }


    public List<User> findAll() {
        return Arrays.asList(USERS);
    }
}
